package game;

public class GameGrid {

    public final int ROWS = 6, COLUMNS = 7;
    private int[][] grid = new int[ROWS][COLUMNS];

    /*
     * Clears the gamegrid for a new game
     */
    public void clearGrid() {
        for (int row = 0; row < ROWS; row++) {
            for (int column = 0; column < COLUMNS; column++) {
                grid[row][column] = 0;
            }
        }
    }

    public int[][] getGameGrid() {
        return grid;
    }

    public int getGridCell(int row, int column) {
        return grid[row][column];
    }

    /*
     * Checks if the move is valid
     */
    public boolean validMove(int column) {
        return (grid[0][column] == 0);
    }

    public int findTopDisc(int coloumn) {
        for (int row = 0; row < ROWS; row++) {
            if (grid[row][coloumn] != 0) {
                return row;
            }
        }
        return 0;
    }

    public boolean placeDisc(int column, int player) {
        for (int row = ROWS - 1; row >= 0; row--) {
            if (isEmptyGridCell(row, column)) {
                setGridCell(row, column, player);
                return true;
            }
        }
        return false;
    }

    private boolean isEmptyGridCell(int row, int column) {
        return grid[row][column] == 0;
    }

    private void setGridCell(int row, int column, int player) {
        grid[row][column] = player;
    }

    public void printGrid() {
        for (int row = 0; row < ROWS; row++) {
            for (int column = 0; column < COLUMNS; column++) {
                System.out.print(grid[row][column] + " ");
            }
            System.out.println("");
        }
    }

    /*
     * Used by the Computer player to test multiple paths. Once a path is fully
     * explored the method is used to bring the grid back to it's original state.
     */
    public void removeLastDisc(int coloumn) {
        for (int i = 0; i < ROWS; i++) {
            if (grid[i][coloumn] != 0) {
                grid[i][coloumn] = 0;
                return;
            }
        }
    }
}
